﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.ShpMana
{
    public partial class wfUpdateRoute : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //修改路线
        private void UpdateRoute()
        {
            string routeId = this.GetParamHtValue("routeId");

            if (string.IsNullOrEmpty(routeId))
            {
                this.ExecJScript(@"layer.alert('该路线不存在.', 3);");
                return;
            }
            string routeTitle = this.GetParamHtValue("routeTitle");
            if (string.IsNullOrEmpty(routeTitle))
            {
                this.ExecJScript(@"layer.alert('标题不能为空.', 3);");
                return;
            }
            string coverageArea = this.GetParamHtValue("coverageArea");
            if (string.IsNullOrEmpty(coverageArea))
            {
                this.ExecJScript(@"layer.alert('请填写涵盖景点.', 3);");
                return;
            }
            string routeJianjie = this.GetParamHtValue("routeJianjie");
            if (string.IsNullOrEmpty(routeJianjie))
            {
                this.ExecJScript(@"layer.alert('请填写路线介绍.', 3);");
                return;
            }

            string scenicSpots = this.GetParamHtValue("scenicSpots");
            if (string.IsNullOrEmpty(scenicSpots))
            {
                this.ExecJScript(@"layer.alert('请填写涵盖景点.', 3);");
                return;
            }
            string characteristic = this.GetParamHtValue("characteristic");
            if (string.IsNullOrEmpty(characteristic))
            {
                this.ExecJScript(@"layer.alert('请填写路线特色.', 3);");
                return;
            }

            string txtInfo = this.GetParamHtValue("txtInfo");
            if (string.IsNullOrEmpty(txtInfo))
            {
                this.ExecJScript(@"layer.alert('请填写简介.', 3);");
                return;
            }

            string countryId = this.GetParamHtValue("countryId");

            string cityId = this.GetParamHtValue("cityId");
            string destId = this.GetParamHtValue("destinationId");
            if (cityId == "0" || countryId == "0")
            {
                this.ExecJScript(@"layer.alert('请选择地区.', 3);");
            }

            string routePrice = this.GetParamHtValue("routePrice");
            if (string.IsNullOrEmpty(routePrice))
            {
                this.ExecJScript(@"layer.alert('请填写价格.', 3);");
                return;
            }
            string djackList = this.GetParamHtValue("djackList");
            string shoutu = this.GetParamHtValue("shoutu");
            if (string.IsNullOrEmpty(shoutu))
            {
                this.ExecJScript(@"layer.alert('请选择首图.', 3);");
                return;
            }
            //地接客
            List<string> djkList = new List<string>();
            if (!string.IsNullOrEmpty(djackList))
            {
                string[] array = djackList.Split(',');
                foreach (string item in array)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        djkList.Add(item);
                    }
                }
                
            }

            using (CBizRoute route = new CBizRoute())
            {
                string[] upRouteMsg = route.UpdateRoute(routeId, routeTitle, txtInfo, characteristic, countryId, cityId, destId, scenicSpots, coverageArea, shoutu, routeJianjie, routePrice, djkList.Count > 0 ? 1 : 0); ;
                if (Convert.ToBoolean(upRouteMsg[0]))
                {
                    using (CBizAdditionalInfo addInfo = new CBizAdditionalInfo())
                    {
                        string adt = addInfo.UpdateItionalInfo(djkList, routeId);
                        if (adt == "OK")
                        {
                            this.ExecJScript("success();");
                        }
                        else {
                            this.ExecJScript("layer.alert('" + adt + ".', 3);");
                        }
                    }
                }
                else
                {
                    this.ExecJScript(@"layer.alert('" + upRouteMsg[1] + ".', 3);");
                }
                
            }

        }


        //获取地接客
        public void GetUserAcction()
        {
            string djkAccunt = this.GetParamHtValue("djkAcction");

            if (string.IsNullOrEmpty(djkAccunt))
            {
                this.ExecJScript("layer.alert('地接客帐号不能为空.', 3);");
                return;
            }
            using (CBizCustomerRegister register = new CBizCustomerRegister())
            {
                DataTable dt = register.GetDjkUserInfo(djkAccunt);
                if (dt == null)
                {
                    this.ExecJScript("$('#djkAccunt').html('<li>没有找到该地接客</li>');");
                    return;
                }
                var djkList = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 20, "");

                this.ExecJScript("loadDjkInfo(" + djkList + ");");
            }

        }

        public void GetRouteInfo()
        {
            string routeId = this.GetParamHtValue("routeId");
            if (string.IsNullOrEmpty(routeId))
            {
                return;
            }
            using (CBizRoute route = new CBizRoute())
            {
                DataSet ds = route.GetRouteInfoByID(routeId);
                DataTable dt = ds.Tables[0];
                if (dt == null)
                {
                    return;
                }
                //加载 主线路信息
                string jsonInfo = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 2, "");
                this.ExecJScript("loadRouteInfo(" + jsonInfo + ")");

                //获取地接客信息
                if (Convert.ToBoolean(dt.Rows[0]["IS_DJKINFO"].ToString()))
                {
                    using (CBizAdditionalInfo addInfo = new CBizAdditionalInfo())
                    {
                        DataTable adt = addInfo.GetItionalInfolList(routeId);
                        if (adt != null)
                        {
                            jsonInfo = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(adt, "", "", 1, 15, "");
                            //加载地接客 信息
                            this.ExecJScript("loadDjk(" + jsonInfo + ")");
                        }
                    }
                }
            }

        }

        public override void DoAction()
        {
            if (this.strAction == "updateRoute")
            {
                UpdateRoute();
            }
            else if (this.strAction == "changeArea")
            {
                RefreshArea();
            }
            else if (this.strAction == "getUser")
            {
                GetUserAcction();
            }
            else if (this.strAction == "init")
            {
                GetRouteInfo();
            }
        }

        private void RefreshArea()
        {
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");

            DataSet ds1, ds2;

            using (msBsc.AppLogic.DataAlc.CBizCountryCity objCBizCountryCity = new AppLogic.DataAlc.CBizCountryCity())
            {
                ds1 = string.IsNullOrEmpty(countryId) ? null : objCBizCountryCity.SelectCountryByParentId(countryId);

                ds2 = string.IsNullOrEmpty(cityId) ? null : objCBizCountryCity.SelectCountryByParentId(cityId);
            }

            if (ds1 != null)
            {
                var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds1.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#country_city_list').html();
                            laytpl(_html).render(" + countryJson + @", function (render) {
                                $('#selCity').html(render);
                            });");
            }

            if (ds2 != null)
            {
                var cityJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds2.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#city_des_list').html();
                            laytpl(_html).render(" + cityJson + @", function (render) {
                                $('#selDestination').html(render);
                            });");
            }
        }
    }
}